<?php

define('WORK_DIR', dirname( __FILE__ ));

/**
 * Преобразует первый символ строки в нижний регистр, только однобайтовая кодировка
 * @param string $string
 * @return string
 */
function lcfirst($string)
{
	$string[0] = strtolower($string[0]);
	return $string;
}

set_include_path(WORK_DIR . DIRECTORY_SEPARATOR . 'ezcomponents' . ((strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? ';' : ':') . ini_get('include_path'));

require('Base/src/base.php');
function __autoload($class_name)
{
	// такое убожество как autoload ezComponents нам не нужен - я не делаю тупую работу - кода на килобайты, а я еще должен какие-то my_autoload.php писать. Если утилита обрастет классами, использую свой эмулятор пространств имен
	if ($class_name[0] == 'e')
	{
		ezcBase::autoload($class_name);
	}
	else
	{
		$fully_qualified_reference = explode('_', $fully_qualified_reference);
		$unqualified_reference = array_shift($fully_qualified_reference);
		if (count($fully_qualified_reference) > 0)
		{
			$package_path = implode(DIRECTORY_SEPARATOR, array_reverse($fully_qualified_reference));
		}
		else
		{
			$package_path = lcfirst($unqualified_reference);
		}

		$last_path = $path[count($path) - 1];
		foreach ($path as $classes_dir)
		{
			$class_file = $classes_dir . DIRECTORY_SEPARATOR . $package_path . DIRECTORY_SEPARATOR . $unqualified_reference;

			if (!file_exists($class_file . '.php'))
			{
				$class_file = $classes_dir . DIRECTORY_SEPARATOR . $package_path . DIRECTORY_SEPARATOR . lcfirst($unqualified_reference) . DIRECTORY_SEPARATOR . $unqualified_reference;
			}
			if ($classes_dir == $last_path OR file_exists($class_file . '.php'))
			{
				include($class_file . '.php');
				break;
			}
		}
		include(WORK_DIR . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . $class_name . '.php');
	}
}

$ConsoleInput = new ezcConsoleInput();

$helpOption = $ConsoleInput->registerOption(new ezcConsoleOption('h', 'help'));
$helpOption->shorthelp = '';
$helpOption->isHelpOption = true;

$inputOption = $ConsoleInput->registerOption(new ezcConsoleOption('i', 'input', ezcConsoleInput::TYPE_STRING));
$inputOption->shorthelp = 'input dir';

$outputOption = $ConsoleInput->registerOption(new ezcConsoleOption('o', 'output', ezcConsoleInput::TYPE_STRING));
$outputOption->shorthelp = 'output file';

$docbookOption = $ConsoleInput->registerOption(new ezcConsoleOption('d', 'docbook', ezcConsoleInput::TYPE_INT));
$docbookOption->shorthelp = 'DocBook version';

$configOption = $ConsoleInput->registerOption(new ezcConsoleOption('c', 'config', ezcConsoleInput::TYPE_STRING));
$configOption->shorthelp = 'config file';

$configOption->addExclusion(new ezcConsoleOptionRule($outputOption));
$configOption->addExclusion(new ezcConsoleOptionRule($inputOption));

$inputOption->addDependency(new ezcConsoleOptionRule($outputOption));
$outputOption->addDependency(new ezcConsoleOptionRule($inputOption));

try
{
	$ConsoleInput->process();
}
catch (ezcConsoleOptionException $e)
{
	exit($e->getMessage());
}

if ($helpOption->value === true)
{
	exit($ConsoleInput->getHelpText('DocBookCompiler 1.0.0'));
}

$config = $ConsoleInput->getOptionValues('long');
if ($configOption->value === false)
{
	$config_dirs = array('.' . DIRECTORY_SEPARATOR . 'docbookcompiler.ini', WORK_DIR . DIRECTORY_SEPARATOR . 'docbookcompiler.ini');
	foreach ($config_dirs as $config_file_candidate)
	{
		if (file_exists($config_file_candidate))
		{
			$config_file = $config_file_candidate;
			break;
		}
	}
}
else
{
	$config_file = $configOption->value;
}
if (isset($config_file))
{
	$Config = new ezcConfigurationIniReader($config_file);
	$Config = $Config->load();
	$config = array_merge($Config->getSettingsInGroup('DocBookCompiler'), $config);
}

if (empty($config))
{
	exit($ConsoleInput->getHelpText('DocBookCompiler 1.0.0'));
}

if (dirname($config['output']) == $config['input'])
{
	exit('Output directory cannot be same input directory');
}

define('DOCBOOK_VERSION', $config['docbook']);

try
{
	$DocBookCompiler = new DocBookCompiler($config['input'], $config['output']);
}
catch (LogicException $e)
{
	exit($e->getMessage() . "\n");
}

?>